<template>
  <div>
    <el-tree
      show-checkbox
      node-key="id"
      check-strictly
      default-expand-all
      :data="list"
      :props="{ label: 'name' }"
      ref="tree"
    />
    <el-row type="flex" justify="end">
      <el-button @click="$emit('close')" type="primary">取消</el-button>
      <el-button @click="hSubmit" type="primary">确定</el-button>
    </el-row>
  </div>
</template>

<script>
import { getPermissionList } from "@/api/permisson";
import { transDataToTreeData } from "@/utils";
import { assignPermission, getRolesDetail } from "@/api/settings";
export default {
  props: {
    curId: {
      type: String,
      required: true,
    },
  },
  data() {
    return {
      list: [],
    };
  },
  created() {
    this.loadAssignPer();
  },
  methods: {
    //表单提交
    async hSubmit() {
      try {
        const permIds = this.$refs.tree.getCheckedKeys();
        const res = await assignPermission({ id: this.curId, permIds:permIds });
        console.log("assignPermission", res);
        this.$emit("close");
      } catch (err) {
        console.log("assignPermission", err);
      }
    },
    async loadAssignPer() {
      try {
        const res = await getPermissionList();
        // console.log('getPermissionList', res)
        const detail = await getRolesDetail(this.curId);
        // console.log(detail);
        this.$refs.tree.setCheckedKeys(detail.data.permIds);
        this.list = transDataToTreeData(res.data);
      } catch (err) {
        console.log("getPermissionList", err);
      }
    },
  },
};
</script>